import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import {DEV_MOON_TOKEN} from "@/constant/ConStant";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    children:[
      {
        path: '/home/file',
        name: 'fileList',
        component: () => import(/* webpackChunkName: "about" */ '../views/pages/FileList.vue')
      }
    ]
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path: '/login',
    name: 'login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue')
  },
  {
    path: '/register',
    name: 'register',
    // route level code-splitting
    component: () => import(/* webpackChunkName: "about" */ '../views/Register.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {

  // const isAuthenticated = store.state.auth.token;
  // const requiresAuth = to.matched.some(record => record.meta.requiresAuth);

   let token =  localStorage.getItem(DEV_MOON_TOKEN)

  console.log(to.path)

  if(to.path === '/login'||to.path === '/register'){
      next()
     return
  }
  if (token === '' || token == null ) {
     next('/login');
     return;
  }

  // else if ((to.path === '/login' || to.path === '/register') && '') {
  //   next();
  // }
    next();



});

export default router
